package com.example.service;

import com.example.entity.MissBook;
import com.example.mapper.MissBookMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/*
帖子业务处理
 */
@Service
public class MissBookService {

    @Resource
    private MissBookMapper missBookMapper;

    public void add(MissBook missBook) {
        MissBook theMissBook=new MissBook();
        theMissBook.setCategory(missBook.getCategory());
        theMissBook.setUid(missBook.getUid());
        theMissBook.setQuestionId(missBook.getQuestionId());
        List<MissBook> findMissBook=selectAll(theMissBook);
        if(findMissBook.size()!=0)
        {
            if(findMissBook.get(0).getStatus().equals("0"))
            {
                return;
            }
            else if(findMissBook.get(0).getStatus().equals("1"))
            {
                findMissBook.get(0).setStatus("0");
                missBookMapper.updateById(findMissBook.get(0));
            }
        }
        else
        {
            missBookMapper.insert(missBook);
        }
    }

    public void deleteById(Integer id) {
        missBookMapper.deleteById(id);
    }

    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids){
            this.deleteById(id);
        }
    }

    public void updateById(MissBook missBook) {
        missBookMapper.updateById(missBook);
    }

    public MissBook selectById(Integer id) {
        return missBookMapper.selectById(id);
    }

    public List<MissBook> selectAll(MissBook missBook) {
        return missBookMapper.selectAll(missBook);
    }

    public PageInfo<MissBook> selectPage(MissBook missBook, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<MissBook> missBookList = missBookMapper.selectAll(missBook);
        return PageInfo.of(missBookList);
    }
}
